<?php
namespace app\common\library;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\RoundBlockSizeMode;
use Endroid\QrCode\Writer\PngWriter;

class QrcodeCreate  
{
    public static function create($url,$size=300 ,$margin=10)
    {
        $writer = new PngWriter();
        // Create QR code
        $qrCode = new QrCode(
            data: $url,
            encoding: new Encoding('UTF-8'),
            errorCorrectionLevel: ErrorCorrectionLevel::Low,
            size: $size,
            margin: $margin,
            roundBlockSizeMode: RoundBlockSizeMode::Margin,
            foregroundColor: new Color(3, 151, 218),
            backgroundColor: new Color(255, 255, 255)
        );
        // Create generic logo
        $logo = new Logo(
            path: public_path().'static'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'xxcy1.png',
            resizeToWidth: 50,
            punchoutBackground: false
        );
        $result = $writer->write($qrCode, $logo);
        return $result->getString();
    }
}